<template>
  <div class="tab">
    <el-tabs ref="tabs" v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="文字链接" name="first">
        <el-table
          :data="list"
          style="width: 100%"
          border
        >
          <el-table-column type="index" label="#" width="50" />
          <el-table-column prop="title" label="标题" />
          <el-table-column prop="des" label="链接描述" />
          <el-table-column prop="type" label="链接类型">
            <el-tag>文字类型</el-tag>
          </el-table-column>
          <el-table-column prop="url" label="链接地址" />
          <el-table-column label="操作" align="center">
            <template #default="{row}">
              <el-row type="flex" justify="center">
                <el-button circle type="primary" icon="el-icon-edit" size="mini" @click="edit(row.id)" />
                <el-button circle type="danger" icon="el-icon-delete" size="mini" @click="del(row.id)" />
              </el-row>
            </template>
          </el-table-column>

        </el-table>
        <el-row type="flex" justify="end">
          <el-pagination
            style="margin-top:10px"
            background
            :current-page="query.page"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="query.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-row>
      </el-tab-pane>
      <el-tab-pane label="图片链接" name="second">
        <el-table
          :data="list"
          style="width: 100%"
          border
        >
          <el-table-column type="index" label="#" width="50" />
          <el-table-column prop="des" label="链接描述" />
          <el-table-column label="链接图片">
            <template #default="{row}">
              <img style="width:150px;" :src="'http://124.223.14.236:8060/' + row.img" alt="">
            </template>
          </el-table-column>
          <el-table-column prop="type" label="链接类型">
            <el-tag type="success">图片类型</el-tag>
          </el-table-column>
          <el-table-column prop="url" label="链接地址" />
          <el-table-column label="操作" align="center">
            <template #default="{row}">
              <el-row type="flex" justify="center">
                <el-button circle type="primary" icon="el-icon-edit" size="mini" @click="edit(row.id)" />
                <el-button circle type="danger" icon="el-icon-delete" size="mini" @click="del(row.id)" />
              </el-row>
            </template>
          </el-table-column>

        </el-table>
        <el-row type="flex" justify="end">
          <el-pagination
            style="margin-top:10px"
            background
            :current-page="query.page"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="query.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-row>
      </el-tab-pane>
      <el-button ref="btn" plain type="primary" class="btn" icon="el-icon-plus" size="mini" @click="openDilog">新增</el-button>

    </el-tabs>
    <!-- ! 弹层 -->
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="35%"
      @close="close"
    >
      <!-- ! 弹层中的表单 -->
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" size="small">
        <el-form-item label="链接类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择">
            <el-option label="文字链接" value="1" />
            <el-option label="图片链接" value="2" />
          </el-select>
        </el-form-item>

        <template v-if="form.type=== '1'">
          <el-form-item label="标题" prop="title">
            <el-input v-model="form.title" placeholder="请输入标题名称" style="width:80%" />
          </el-form-item>
        </template>

        <el-form-item label="链接地址" prop="url">
          <el-input v-model="form.url" placeholder="请输入链接地址名称" style="width:80%" />
        </el-form-item>
        <el-form-item label="链接描述" prop="des">
          <el-input v-model="form.des" type="textarea" placeholder="请输入链接描述名称" style="width:80%" />
        </el-form-item>
        <template v-if="form.type=== '2'">
          <el-form-item label="链接图片">
            <el-upload
              ref="upload"
              v-model="form.img"
              class="upload-demo"
              action="http://124.223.14.236:8060/"
              :on-success="success"
              :file-list="fileList"
              list-type="picture"
            >
              <el-button size="small" type="primary">点击上传</el-button>
            </el-upload>
          </el-form-item>
        </template>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>

</template>
<script>

import { getLink, addLink, deleteLink, getLinkDetail, editLink } from '@/api/adv/link'
import { getImg } from '@/api/adv/img'
export default {

  data() {
    return {
      form: {
        des: '',
        img: '',
        title: '',
        type: '',
        url: ''
      },
      rules: {
        des: [
          { required: true, message: '链接描述类型必须', trigger: 'blur' }
        ],
        title: [
          { required: true, message: '链接标题类型必须', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '链接类型类型必须', trigger: 'blur' }
        ],
        url: [
          { required: true, message: '链接地址类型必须', trigger: 'blur' }
        ]
      },
      activeName: 'first',
      list: [],
      query: {
        type: '1',
        page: 1,
        limit: 10
      },
      total: 0,
      dialogVisible: false,
      fileList: []
    }
  },
  computed: {
    title() {
      return this.form.id ? '修改友情链接' : '添加友情链接'
    }
  },
  created() {
    this.initData()
  },
  mounted() {
    const scrollBar = this.$refs.tabs.$el.querySelector('.el-tabs__nav-scroll')
    scrollBar.appendChild(this.$refs.btn.$el)
  },
  methods: {

    // ! 删除
    del(id) {
      this.$confirm('确定删除吗,是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // ! 删除请求

        await deleteLink(id)
        this.initData()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // ! 图片上传
    async success(response, files, fileList) {
      const file = fileList[0].raw
      console.log(file)

      const fd = new FormData()

      fd.append('file', file)
      const res = await getImg(fd)
      this.form.img = res.savePath
      console.log(this.form.img)
    },
    // ! 编辑回显
    async edit(id) {
      const res = await getLinkDetail(id)
      this.form = res
      this.dialogVisible = true
      // ! 图片回显
      const obj = { name: '点击查看详情', url: 'http://124.223.14.236:8060/' + res.img }
      this.fileList.push(obj)
    },
    // ! 提交表单
    submit() {
      this.$refs.form.validate(async isOk => {
        if (!isOk) return
        if (this.form.id) {
          await editLink(this.form)
          this.$message.success('修改成功')
        } else {
          await addLink(this.form)
          this.$message.success('添加成功')
        }
        this.initData()
        this.close()
      })
    },
    // ! 弹层打开
    async openDilog() {
      this.dialogVisible = true
    },
    // ! 弹层关闭
    close() {
      this.dialogVisible = false
      this.form = {
        des: '',
        img: '',
        title: '',
        type: '',
        url: ''
      }
      // ! 清空文件数据中的数据
      this.fileList = []
      this.$refs.form.clearValidate()
    },
    // ! 分页
    handleSizeChange(value) {
      this.query.limit = value
      this.query.page = 1
      this.initData()
    },
    handleCurrentChange(value) {
      this.query.page = value
      this.initData()
    },
    handleClick(tab, event) {
      if (this.activeName === 'first') {
        this.query.type = '1'
      }
      if (this.activeName === 'second') {
        this.query.type = '2'
      }
      this.initData()
    },
    async initData() {
      if (this.activeName === 'second') {
        this.query.type = '2'
      }
      const res = await getLink(this.query)
      this.list = res.data
      this.total = res.count
    }
  }
}
</script>
<style lang="scss" scoped>
  .tab{
    margin: 20px;
    padding: 20px;
    background-color: #fff;
  }
  .el-tabs__nav-scroll {
  display: flex;
  justify-content: space-between;

}
.btn{
  position: absolute;
  top: 0px;
  right: 0px;
}
  ::v-deep .el-dialog__header{
   padding: 15px 20px 10px!important;
    border-bottom: 1px solid #e9ecef;
    background-color: #fafafa;
  }
</style>
